package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.DequipmentStaBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InfrarePictureAdapter extends RecyclerView.Adapter<InfrarePictureAdapter.ActivityHolder> implements View.OnClickListener {

    private Context context;
    private List<DequipmentStaBean.DataBean> mData;
    private OnItemClickListener mOnItemClickListener = null;
    private int screenWidth;
    private float enlargeNum = 0;

    public InfrarePictureAdapter(Context context, List<DequipmentStaBean.DataBean> data) {
        this.context = context;
        this.mData = data;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_infrare_picture, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                DequipmentStaBean.DataBean listBean = mData.get(position);
                String name = listBean.getCameraName();
                int totalNum = listBean.getTotal();
                int validNum = listBean.getValidNum();
                int invalidNum = totalNum - validNum;
                float ratio = listBean.getRatio();


                if (!StringUtils.isEmpty(name)) {
                    holder.tvName.setText(name + "");
                }

                holder.tvNumInvalid.setText(invalidNum + "");
                holder.tvNumValid.setText(validNum + "");

                if (totalNum != 0) {

                    int spaceWidth = screenWidth - SizeUtils.dp2px(180);
                    if (position == 0) { //将第一条数据的宽度进行放大
                        int width = (int) (spaceWidth * ratio);
                        enlargeNum = spaceWidth / (float) width; //放大的倍数
                    }


                    int totalWidth = (int) (spaceWidth * ratio * enlargeNum);
                    ViewGroup.LayoutParams linearParams1 = (ViewGroup.LayoutParams) holder.tvNumInvalid.getLayoutParams();
                    linearParams1.width = (totalWidth * invalidNum) / totalNum + SizeUtils.dp2px(15);
                    linearParams1.height = SizeUtils.dp2px(20);
                    holder.tvNumInvalid.setLayoutParams(linearParams1);
                    holder.tvNumInvalid.setBackgroundColor(context.getResources().getColor(R.color.bg_color_f7c));
                    holder.tvNumInvalid.setTextColor(context.getResources().getColor(R.color.white));

                    ViewGroup.LayoutParams linearParams2 = (ViewGroup.LayoutParams) holder.tvNumValid.getLayoutParams();
                    linearParams2.width = (totalWidth * validNum) / totalNum +  SizeUtils.dp2px(15);
                    linearParams2.height = SizeUtils.dp2px(20);
                    holder.tvNumValid.setLayoutParams(linearParams2);
                    holder.tvNumValid.setVisibility(View.VISIBLE);
                } else {

                    ViewGroup.LayoutParams linearParams1 = (ViewGroup.LayoutParams) holder.tvNumInvalid.getLayoutParams();
                    linearParams1.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                    linearParams1.height = SizeUtils.dp2px(20);
                    holder.tvNumInvalid.setLayoutParams(linearParams1);
                    holder.tvNumInvalid.setBackgroundColor(context.getResources().getColor(R.color.transparent));
                    holder.tvNumInvalid.setTextColor(context.getResources().getColor(R.color.text_color333));
                    holder.tvNumValid.setVisibility(View.GONE);

                }

            }
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void upData(List<DequipmentStaBean.DataBean> data, int screenWidth) {
        mData = data;
        this.screenWidth = screenWidth;
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_num_invalid)
        TextView tvNumInvalid;
        @BindView(R.id.tv_num_valid)
        TextView tvNumValid;

        public ActivityHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


}
